Ext.define('appmod.controller.resources.module.Startup', {
    extend: 'Ext.app.Controller',

	models: [

	],
	
    stores: [
		'resources.module.RegPaging',
		'resources.module.AllFuns'
	],
    

    refs: [
		{ref: 'modulesList',selector:'module-dashboard gridpanel[region=center]'},
		{ref: 'moduleFunsList',selector:'module-dashboard gridpanel[region=east]'}

	],

    views: [
		'resources.module.Dashboard'
    ],
	

    init: function() {
		var me = this;
    	me.control({
			'module-dashboard': {
			    reflashModuleFunctions:{
					fn:me.clickAndSelectModule,
					scope:me
				}
			},			
			'module-dashboard gridpanel[region=east] button[action=updateFunBootup]': {
			    click: {
					fn:me.updateFunBootup,
					scope:me
				}
			},
			'moduledashboard': {
			    moduleDelete:me.deleteModule
			}
			
								
    	});

    },
	
	
	/**
	 * load main compont in this controller to framework container
	 */
	loadInitComp : function() {
		var mainPanel = Ext.create('appmod.view.resources.module.Dashboard' , {
			region : 'center',
			border:false,
			padding:'0 0 0 0'
		});
		return mainPanel;		
	},
	

	// --- use this app lication
	onLaunch:function(app) {

		var me = this;
		
		var modulesList = me.getModulesList();
		var dsOfModList = me.getResourcesModuleRegPagingStore();
		modulesList.bindStore(dsOfModList);

		var modFunsList = me.getModuleFunsList();
		var dsOfFunList = me.getResourcesModuleAllFunsStore();
		modFunsList.bindStore(dsOfFunList);

	},

	
	clickAndSelectModule:function (grid, eOpts ) {
		var me = this;
		record = eOpts['record'];

		var recName = record.get('name');
		var recId = record.getId();
		
		// --- get another panel store ---
		var functionPanel = me.getModuleFunsList();
		var funPanelStore = functionPanel.getStore();
		var proxy = funPanelStore.getProxy();

		proxy.extraParams = {
			modResId : recId
		};

		funPanelStore.load();


	},

	updateFunBootup:function() {
		var me = this;
		var store = me.getResourcesModuleAllFunsStore();

		var proxy =store.getProxy();
		store.sync({
			callback:function(batch , options) {
				//alert('callback');
			},
			success:function(batch , options) {
				if ( batch.isComplete) {
					Ext.MessageBox.show({
        				title: "提示",
        				msg: '功能启用更新',
        				buttons: Ext.MessageBox.OK,
        				icon: Ext.MessageBox.WARNING
       				});
				}
			},
			failure:function(batch , options) {
				//alert('failure');
			}
		});


	},

	deleteModule:function(grid , options) {
		var me = this;
		var store = grid.getStore();
		var rec = grid.getStore().getAt(options.rowIndex);
		Ext.MessageBox.confirm('确认', '删除模块时，会连同相关的资源和权限均被彻底删除，不能恢重。确定删除: "'+rec.get('name')+'"?',function() {
			me.deleteLoadedModules(me , grid, rec);
		}, me);

	},
	
	deleteLoadedModules:function(me , grid, removedRecord) {

		var jsonData = [];
		jsonData.push( removedRecord.getId() );

		// --- update record using ajax
		try {

			Ext.Ajax.request({
				url: '../../data/resource/resources/submit_deleteLoadedModules.json',
				method:'post',
				params:{
					'jsonData':Ext.encode(jsonData)
				},
				success: function(response, opts) {

					 var obj = Ext.decode(response.responseText);
					 
					 // --- popup message ---
					Ext.MessageBox.show({
        				title: "提示",
        				msg: obj.msg,
        				buttons: Ext.MessageBox.OK,
        				icon: Ext.MessageBox.WARNING
       				});
					
					// --- reload ---
					

				},
				failure: function(response, opts) {
				
				}
			});

			
		} catch (e) {
			alert(e);
		}		

	}
	

	
});